package kr.hyunist.androidcomplete01saeildamil.ch19;

import kr.hyunist.androidcomplete01saeildamil.*;
import android.app.*;
import android.content.*;
import android.content.DialogInterface.OnClickListener;
import android.os.*;
import android.view.*;
import android.widget.*;

public class LongTimeEx03 extends Activity {
	int mValue;
	TextView mText;
	ProgressDialog mProgress;
	boolean mQuit;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.longtimeex);
		
		mText = (TextView) findViewById(R.id.longtimetv);
	}
	
	@SuppressWarnings("deprecation")
	public void mOnClick(View v){
		mValue = 0;
		showDialog(0);
		mQuit = false;
		mHandler.sendEmptyMessage(0);
	}
	
	@SuppressWarnings("deprecation")
	protected Dialog onCreateDialog(int id) {
		
		switch(id){
		case 0:
			mProgress = new ProgressDialog(this);
			mProgress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
			mProgress.setTitle("Updating");
			mProgress.setMessage("Wait...");
			mProgress.setCancelable(false);
			mProgress.setButton("Cancel", new OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					mQuit = true;
					dismissDialog(0);
				}
				
			});
			return mProgress;
		}
		return null;
		
	};
	
	Handler mHandler = new Handler(){
		@SuppressWarnings("deprecation")
		public void handleMessage(Message msg) {
			mValue++;
			mText.setText(Integer.toString(mValue));
			
			try {
				Thread.sleep(50);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			
			if(mValue < 100 && mQuit == false){
				mProgress.setProgress(mValue);
				mHandler.sendEmptyMessage(0);
			} else {
				dismissDialog(0);
			}
		};
	};
}
